#include<iostream>
using namespace std;
int N = 10e9;
void DIJ(int (&graph)[200][200], int (&shortpath)[2][200], int n,int Q)
{
	int i = 0;
	int cur = Q;
	int flag = n;
	while (flag)
	{
		int mini = 0;
		int min = N;
		for (i = 0; i < n; i++)
		{
			if (graph[cur][i] != 0 && shortpath[0][i] == 0)
			{
				if (graph[cur][i]+shortpath[1][cur] < shortpath[1][i])
				{
					shortpath[1][i] = graph[cur][i] + shortpath[1][cur];
				}
				
			}
			if (shortpath[1][i] < min&&shortpath[0][i]!=1)
			{
				min = shortpath[1][i];
				mini = i;
			}
		}
		shortpath[0][mini] = 1;
		flag--;
		cur = mini;
	}
	
}
int main()
{
	int graph[200][200] = { 0 };
	int shortpath[2][200] = { 0 };
	int n,Q;
	cin >> n >> Q;
	int i = 0;
	for (i; i < n; i++)
	{
		int j=0;
		for (j = 0; j < n; j++)
		{
			cin >> graph[i][j];
		}
		shortpath[1][i] = N;
	}
	shortpath[0][Q] = 1;
	shortpath[1][Q] = 0;
	DIJ(graph, shortpath, n,Q);
	for (i = 0; i < n; i++)
	{
		if (i != Q)
		{
			if (shortpath[1][i] == N)
				cout << "-1 ";
			else
			cout << shortpath[1][i] << ' ';
		}
	}
}